<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="hongbao" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 组织架构主体部分 -->
        <el-tree default-expand-all :data="list">
          <!-- 不是每个作用域插槽都是row，具体是什么得根据实际情况打印查看 -->
          <template v-slot="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!--
      :close-on-click-modal="false" 不允许点击遮罩层关闭
      :close-on-press-escape="false" 不允许按 ESC 关闭
      @closed="$refs.deptDialog.clearForm()"
       -->
    <el-dialog
      :title="isEdit ? '编辑': '添加'"
      :visible.sync="showDialog"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="hClose" />
      <!-- <add-or-edit :id="curId" ref="deptDialog" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="hClose" /> -->
    </el-dialog>

    <!-- <el-dialog
      :title="isEdit ? '编辑': '添加'"
      :visible.sync="showDialogEdit"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="showDialogEdit" :id="curId" :is-edit="isEdit" @success="hSuccess" />
    </el-dialog> -->
  </div>
</template>
<script>
import { deleteDepartmentById, getDepartmentsList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      // showDialogEdit: false
      originList: []
    }
  },
  created() {
    this.loadDepartmentsList()
  },
  methods: {
    async loadDepartmentsList() {
      try {
        const res = await getDepartmentsList() // 发请求获取平铺数组
        // console.log(res.data.depts)
        res.data.depts.shift() // 删除最前面的传智教育公司
        // console.log(res.data.depts) // 打印获取的平铺数组数据

        // 从平铺数组里面抽取需要的数据放到新的数组里面
        // 写法一
        // this.originList = res.data.depts.map(item => {
        //   return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        // })

        // 写法二
        // this.originList = res.data.depts.map(item => ({ id: item.id, pid: item.pid, name: item.name, code: item.code }))

        // 写法三:高级写法
        this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
        this.list = tranListToTreeData(res.data.depts) // 平铺数组转树形结构
      } catch (e) {
        console.log(e)
      }
    },
    hShowAdd(id) {
      // 显示添加的dialog
      this.showDialog = true
      // console.log(data.id)
      // 获取当前被点击的部门id，传给子组件
      this.curId = id
      // 将状态修改为添加
      this.isEdit = false
    },
    hShowEdit(id) {
      // 显示编辑的dialog
      // this.showDialogEdit = true
      this.showDialog = true
      // console.log(data.id)
      // 获取当前被点击的部门id，传给子组件
      this.curId = id
      // 将状态修改为添加
      this.isEdit = true
      // 调用子组件的方法获取数据
      // 原因:数据修改后，DOM 更新是异步的
      // this.$nextTick(() => { // 等待下一次 DOM 更新后执行
      //   this.$refs.deptDialog.loadDepartmentDetail()
      // })
    },
    hSuccess() {
      // 关闭dialog
      this.showDialog = false
      // this.showDialogEdit = false
      // 发请求获取最新的数据
      this.loadDepartmentsList()
    },
    hDel(id) {
      // 1.删除之前要提示用户是否要删除
      this.$confirm('真的要删除吗？', '提示', {
        // 点击遮罩层不关闭 confirm 框
        closeOnClickModal: false,
        // 类型
        type: 'warning'
      })
        // 2，删除之后要发请求更新数据
        .then(async() => {
          try {
          // 发请求
            const res = await deleteDepartmentById(id)
            // 提醒用户
            this.$message.success(res.message)
            // 重新渲染页面
            this.loadDepartmentsList()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    },
    hClose() {
      // 关闭 dialog
      this.showDialog = false
    }
  }
}
</script>
